package cn.kgc.crowdfundingadmin.service;

import cn.kgc.crowdfundingadmin.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadmin.mapper.AdminMapper;
import cn.kgc.crowdfundingadmin.pojo.Admin;
import cn.kgc.crowdfundingadmin.pojo.AdminExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author niejianhang
 * @create 2020-02-19 15:10
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    AdminMapper adminMapper;


    @Override
    public Admin selById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public Admin login(String loginacct, String userpswd) {
        //声明实体类
        Admin admin = null;
        //实例化AdminExample类
        AdminExample example = new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andLoginacctEqualTo(loginacct);
        //获取根据账号list集合
        List<Admin> list = adminMapper.selectByExample(example);
        if (CrowdFundingUtils.collectionEffective(list)) {
            //admin获得数据
            admin = list.get(0);
            //将输入userpswd的转化md5加密
            String md5 = CrowdFundingUtils.md5(userpswd);
            //比较登录
            if (md5.equals(admin.getUserpswd())) {
                //登录成功
            } else {
                admin = null;
            }
        }
        return admin;
    }


    @Override
    public PageInfo<Admin> queryForKeywordsearch(Integer pageNum, Integer pageSize, String keyword) {
        //设置当前页
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("id");
        //keyword没有值查全部，有值根据账号或者名称或者邮箱地址模糊查询
        if (CrowdFundingUtils.stringEffective(keyword)) {
            AdminExample example = new AdminExample();
            AdminExample.Criteria criteria1 = example.createCriteria();
            criteria1.andCreatetimeLike("%" + keyword + "%");
            AdminExample.Criteria criteria2 = example.createCriteria();
            criteria2.andUsernameLike("%" + keyword + "%" );
            AdminExample.Criteria criteria3 = example.createCriteria();
            criteria3.andEmailLike("%" + keyword + "%" );
            example.or(criteria2);
            example.or(criteria3);
            List<Admin> list = adminMapper.selectByExample(example);
            PageInfo<Admin> pageInfo = new PageInfo<>(list);
            return pageInfo;
         } else {
            //查询全部
            List<Admin> list = adminMapper.selectByExample(null);
            PageInfo<Admin> pageInfo = new PageInfo<>(list);
            return pageInfo;
        }
    }

    @Override
    public void batchRemove(List<Integer> adminIdList) {
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(adminIdList);

        adminMapper.deleteByExample(example);
    }

    @Override
    public void saveAdmin(Admin admin) {
        String userPswd = admin.getUserpswd();
        userPswd = CrowdFundingUtils.md5(userPswd);
        admin.setUserpswd(userPswd);
        adminMapper.insert(admin);
    }

    @Override
    public Admin getAdminById(Integer adminId) {
        return adminMapper.selectByPrimaryKey(adminId);
    }

    @Override
    public void updateAdmin(Admin admin) {
        // 对密码进行加密
        if (CrowdFundingUtils.stringEffective(admin.getUserpswd())){
            String userPswd = admin.getUserpswd();
            userPswd = CrowdFundingUtils.md5(userPswd);
            admin.setUserpswd(userPswd);
        }
        adminMapper.updateByPrimaryKeySelective(admin);

    }


}